# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo, os, requests


class JinritoutiaoPipeline(object):
    def __init__(self):
        myclient = pymongo.MongoClient('127.0.0.1', 27017)
        if '今日头条' in myclient.list_database_names():
            myclient.drop_database('今日头条')
        mydb = myclient['今日头条']
        self.mycol = mydb['今日头条']
        self._id = 1

    def process_item(self, item, spider):
        item['_id'] = self._id
        mydict = {
            '_id': item['_id'],
            '分类名称': item['type_name'],
            '分类链接': item['type_url'],
        }
        self.mycol.insert_one(mydict)
        self._id += 1
        return item


class JinritoutiaoPipeline_news(object):
    def process_item(self, item, spider):
        os.makedirs(item['news_title'], exist_ok=True)
        img = requests.get(item['news_img'])
        with open(item['news_title'] + '/' + item['news_title'] + '.jpg', 'wb') as f:
            f.write(img.content)
        return item
